package concurrent;

import java.util.concurrent.Semaphore;

// 也可以用volatile 变量模拟自旋锁
public class PrintInOrder1114 {
    class Foo {
        Semaphore sem1;
        Semaphore sem2;

        public Foo() {
            sem1 = new Semaphore(0);
            sem2 = new Semaphore(0);
        }

        public void first(Runnable printFirst) throws InterruptedException {
            // printFirst.run() outputs "first". Do not change or remove this line.
            printFirst.run();
            sem1.release(1);
        }

        public void second(Runnable printSecond) throws InterruptedException {
            sem1.acquire(1);
            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            sem2.release(1);
        }

        public void third(Runnable printThird) throws InterruptedException {
            sem2.acquire(1);
            // printThird.run() outputs "third". Do not change or remove this line.
            printThird.run();
        }
    }
}
